﻿using System.IO;

namespace Raven
{
    public class TriggerHealthGiver : TriggerRespawning<RavenBot>
    {
        private Fix _healthGiven;
        protected DisplayObject _displayObject;

        public TriggerHealthGiver(BinaryReader reader) : base(EntityType.Health)
        {
            Read(reader);
            CreateDisplayObject();
        }

        private void Read(BinaryReader reader)
        {
            Fix x = new Fix(reader.ReadInt32());
            Fix y = new Fix(reader.ReadInt32());
            Fix r = new Fix(reader.ReadInt32());
            _healthGiven = new Fix(reader.ReadInt32());
            int GraphNodeIndex = reader.ReadInt32();

            SetPos(new Vector2(x, y));
            SetBRadius(r);
            SetGraphNodeIndex(GraphNodeIndex);

            //create this trigger's region of fluence
            AddCircularTriggerRegion(Pos(), Constants.DefaultGiverTriggerRange);

            SetRespawnDelay(Constants.Health_RespawnDelay);
        }

        public override void Try(RavenBot entity)
        {
            if (isActive() && isTouchingTrigger(entity.Pos(), entity.BRadius()))
            {
                entity.IncreaseHealth((int) _healthGiven);

                Deactivate();
            }
        }

        public void CreateDisplayObject()
        {
            _displayObject = DisplayObjectFactory.CreateDisplayObject(this);
            _displayObject.Start();
        }
        
        public override void Update()
        {
            base.Update();
            _displayObject.Update();
        }

        public override void Destroy()
        {
            _displayObject.Destroy();
        }
    }
}